package cn.edu.hstc.wm.ui.fragment;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.hstc.wm.AppConfigs;
import cn.edu.hstc.wm.Constants;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.bean.Good;
import cn.edu.hstc.wm.service.AppConfigService;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 显示点击菜单后的详细内容
 * 
 */
public class StoreItemMenuInfoDialog extends DialogFragment implements OnClickListener {
	public static final String TAG = "StoreItemMenuInfoDialog";
	private static StoreItemMenuInfoDialog sDialog = null;
	private static Bundle sBundle = null;
	private static Good sGoods = null;

	DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

	private View rootView;

	private ImageView cancelView, shareView;
	private RatingBar grade;
	private ImageView iconView;
	private TextView textView;

	/**
	 * 获取显示菜单详细信息的对话框实例
	 * 
	 * @param bundle
	 * @return
	 */
	public static StoreItemMenuInfoDialog newInstance(Bundle bundle) {
		if (sDialog == null || sBundle != bundle) {
			sDialog = new StoreItemMenuInfoDialog();
			sBundle = bundle;
		}
		return sDialog;
	}

	public static StoreItemMenuInfoDialog newInstance(Good good) {
		if (sDialog == null || sGoods != good) {
			sDialog = new StoreItemMenuInfoDialog();
			sGoods = good;
		}
		return sDialog;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");
		if (getDialog() != null) {
			getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); // 设置对话框无标题
			getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent); // 设置背景透明
		}
		initImageLoader();
		// 设置为dialog范围外点击将取消
		getDialog().setCanceledOnTouchOutside(true);
		rootView = inflater.inflate(R.layout.store_item_menu_info_dialog_layout, container, false);
		initView();
		initData();
		return rootView;
	}

	private void initImageLoader() {
		// 图片 显示的配置样式
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.default_no_img)// 设置图片下载期间显示的图片
				.showImageForEmptyUri(R.drawable.default_no_img)// 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.default_no_img)// 设置图片加载或解码过程中发生错误显示的图片
				.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
				.cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中
				.considerExifParams(true).displayer(new RoundedBitmapDisplayer(5))// 设置成圆角图片
				.build();// 创建配置过得DisplayImageOption对象
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return super.onCreateDialog(savedInstanceState);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	@Override
	public void onStart() {
		super.onStart();
		if (getDialog() != null) {
			// 获取宽度
			int fullWidth = getDialog().getWindow().getAttributes().width;
			// 不同版本api的方法
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
				Display display = getActivity().getWindowManager().getDefaultDisplay();
				Point size = new Point();
				display.getSize(size);
				fullWidth = size.x;
			} else {
				Display display = getActivity().getWindowManager().getDefaultDisplay();
				fullWidth = display.getWidth();
			}

			final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());

			// 有效显示区域
			int width = fullWidth - padding * 2;
			int height = getDialog().getWindow().getAttributes().height;
			// getDialog().getWindow().setLayout(width, height); // 设置位置
			getDialog().getWindow().setLayout(width, (int) (width * 1.5));
			Log.d("", "fullWidth = " + fullWidth + "   width = " + width + "   height = " + height);

		}
	}

	private void initView() {
		Log.d(TAG, sGoods.toString());
		cancelView = (ImageView) rootView.findViewById(R.id.iv_store_item_menu_info_dialog_cancel);
		shareView = (ImageView) rootView.findViewById(R.id.iv_store_item_menu_info_dialog_share);
		iconView = (ImageView) rootView.findViewById(R.id.iv_store_item_menu_info_dialog_icon);
		textView = (TextView) rootView.findViewById(R.id.tv_store_item_menu_info_dialog_info);

		cancelView.setOnClickListener(this);
		shareView.setOnClickListener(this);

	}

	/** 初始化显示的数据 */
	private void initData() {
		String imageName = sGoods.getImageName();
		if (AppConfigService.isLoadImage(getActivity()) && !imageName.equals(Constants.DEFAULT_IMG)) {
			// 允许从网络加载图片
			ImageLoader.getInstance().displayImage(AppConfigs.GOOD_IMAGE_URL + imageName, iconView);
		} else {
			// 使用默认的图片
			iconView.setImageResource(R.drawable.default_no_img);
		}
		// TODO Goods 添加属性getInfo
		// if (sGoods.getInfo.size>0) {
		// textView.setText(sGoods.getInfo());

		// }
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_store_item_menu_info_dialog_cancel:
			sDialog.dismiss();
			break;
		case R.id.iv_store_item_menu_info_dialog_share:
			share();
			break;
		default:
			break;
		}
	}

	/**
	 * 分享内容
	 */
	public void share() {
		String shareContent = "Hi,我给大家分享个好吃的哟!~.~\n就是它了    " + sGoods.getName();
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent);
		shareIntent.setType("text/plain");
		startActivity(Intent.createChooser(shareIntent, "分享到..."));
	}

	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		private static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				// 是否第一次显示
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					// 图片淡入效果
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
